package cn.tworice.blog.service.impl;

import cn.tworice.blog.dao.CommentDao;
import cn.tworice.blog.entity.SendComment;
import cn.tworice.blog.entity.SendMailDto;
import cn.tworice.blog.service.ReceiveCommentService;
import cn.tworice.blog.utils.MailUtil;
import com.alibaba.fastjson.JSON;
import lombok.extern.slf4j.Slf4j;
import org.springframework.amqp.rabbit.annotation.RabbitListener;
import org.springframework.amqp.rabbit.core.RabbitTemplate;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;

@Service
@Slf4j
public class ReceiveCommentServiceImpl implements ReceiveCommentService {

    final MailUtil mailUtil;
    @Autowired
    public ReceiveCommentServiceImpl(MailUtil mailUtil){
        this.mailUtil=mailUtil;
    }

    @Autowired
    RabbitTemplate rabbitTemplate;

    @Autowired
    private CommentDao commentDao;


    @RabbitListener(queues = "comment_queue")
    @Override
    public void receiveComment(String message) {
        log.info("收到消息"+message);

        SendComment sendComment = JSON.toJavaObject(JSON.parseObject(message), SendComment.class);
        commentDao.insertComment(sendComment);
    }

    /**
     * 发送邮箱
     * @author 二饭
     * @email 1786705138@qq.com
     */
    @RabbitListener(queues = "mail_queue")
    @Override
    public void receiveMail(String sendMailDto) {

        SendMailDto parse = JSON.toJavaObject(JSON.parseObject(sendMailDto), SendMailDto.class);
        log.info("收到消息-给用户发送邮件"+parse.getTo());
        try {
                mailUtil.sendHTMLMail(parse);
        }catch (Exception ex){
                System.out.println("报错"+ex);
        }

    }





}
